package com.jt.sevrice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.dao.ItemDescMapper;
import com.jt.dao.ItemMapper;
import com.jt.dao.ItemParamMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.pojo.ItemParam;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service

public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ItemParamMapper itemParamMapper;
    @Override
    public PageResult getItemList(PageResult pageResult) {
        IPage iPage =new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Item>queryWrapper=new QueryWrapper<>();
        String query = pageResult.getQuery();
        boolean b = StringUtils.hasLength(query);
        queryWrapper.eq(b,"title",query);
        IPage iPage1 = itemMapper.selectPage(iPage, queryWrapper);
        long total = iPage1.getTotal();
        List<Item>records = iPage1.getRecords();
        return pageResult.setTotal(total).setRows(records);
    }

    @Override
    public void saveItem(ItemVO itemVO) {
        Item item = itemVO.getItem();
        ItemDesc itemDesc = itemVO.getItemDesc();
        ItemParam itemParam = itemVO.getItemParam();
        itemMapper.insert(item);
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);
        itemParamMapper.insert(itemParam);
    }
}
